<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 https://open.pinduoduo.com/application/document/browse?idStr=BD3A776A4D41D5F5
 https://open.pinduoduo.com/application/document/api?id=pdd.goods.add
*/
namespace jy_duodian;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function __construct(){
		$this->wxapp=$this->plugin_setting('basic');
		$dianappid=intval($basic['appid']);
		if(!$dianappid){
			$dianappid=$_SESSION['dianappid'];
		}
		if(empty($this->wxapp['client_id'])){
			exi('未填写小店ID','error');
		}
		if(empty($this->wxapp['secret'])){
			//exi('未填写小店secret','error');
		}
		if($this->wxapp['expire']<TIMESTAMP || !$this->wxapp['access_token']){
			//$this->getAccessToken();
		}
	}
	public function getAccessToken($continue=false,$noexi=0){//获取access_token
		global $_AR;
		return '';
		$noexi=$noexi?:$_SESSION['noexi'];
		if($this->wxapp['expire']>TIMESTAMP && !$continue && $this->wxapp['access_token']){
			return $this->wxapp['access_token'];
		}
		if(!$this->wxapp['secret']){
			return false;
		}else{
			$url = "https://fuwu.pinduoduo.com/cgi-bin/token?grant_type=client_credential&appid={$this->wxapp['appid']}&secret={$this->wxapp['secret']}";
		}
		$content = cfc('http')->ihttp_request($url);
		if(is_error($content) && !$noexi){
			exi('获取微信小店授权失败, 请稍后重试！错误详情：'.$content['message'],'error');
		}
		$token = @json_decode($content['content'], true);
		if(empty($token) || empty($token['access_token']) && !$noexi) {
			exi('获取微信小店授权失败, 请稍后重试！错误代码-' . $token['errcode'] . '，错误信息-' . $token['errmsg'].'错误详情：'.$this->error_code($token['errcode']),'error');
		}
		$this->wxapp['access_token']=$token['access_token'];
		$this->wxapp['expire'] = TIMESTAMP + $token['expires_in'] - 200;
		$this->plugin_setting('basic',$this->wxapp);
		return $token['access_token'];
	}
	//发起http请求
	public function request($url,$post=null,$continue=true,$direct=false,$return_error=false,$isjson=false,$isrequest=false){
		if($this->return_error_one){
			$return_error=true;
			$this->return_error_one=false;
		}
		if($this->return_error){
			$return_error=true;
		}
		$rurl=str_replace(array('{access_token}','ACCESS_TOKEN'),$this->wxapp['access_token'],$url);
		if($isjson){
			$headers = array('Content-Type' => 'application/json');
			$result=cfc('http')->ihttp_json($rurl,$post);
		}elseif($isrequest){
			$result=cfc('http')->ihttp_request($rurl,$post);
		}elseif($post){
			$result=cfc('http')->ihttp_post($rurl,$post);
		}else{
			$result=cfc('http')->ihttp_get($rurl);
		}
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		if(($data['errcode']=='40001' || $data['errcode']=='42001') && $continue){
			$this->getAccessToken(true);
			$data=$this->request($url,$post,false,$direct,$return_error);
			return $data;
		}
		
		if($data['errcode']){
			$message=$this->error_code($data['errcode']).' '.$data['errmsg'].' '.$data['errcode'];
			if($return_error || $_SESSION['this']=='custom'){
				//return error($message);
			}else{
				//exi($message,'error');
			}
		}
		if($direct){
			return $result;
		}
		if(!is_array($data) || !$data){
			$message='访问小店接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		return $data;
	}
	//给第三方插件提供的额外接口，用于获取access_token
	public function access_token(){
		return $this->getAccessToken();
	}
	//获取店铺基本信息
	public function get_shopinfo() {
		$url = "https://gw-api.pinduoduo.com/api/router/ec/basics/info/get?access_token={access_token}";
		$res=$this->request($url,array('from'=>'jy'));
		if($res['errcode']){
			exi('获取店铺基本信息失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['info'];
	}
	//获取类目详情
	public function get_category() {
		$url = "https://gw-api.pinduoduo.com/api/router/ec/category/all?access_token={access_token}";
		$res=$this->request($url,array('from'=>'jy'));
		if($res['errcode']){
			exi('获取类目详情失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['cats'];
	}
	//获取店铺的商品分类
	public function get_shopcat() {
		$url = "https://gw-api.pinduoduo.com/api/router/ec/store/classification/tree/get?access_token={access_token}";
		$res=$this->request($url,array('from'=>'jy'));
		if($res['errcode']){
			exi('获取店铺的商品分类失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['resp'];
	}
	//获取品牌
	public function get_brands() {
		$url = "https://api.weixin.qq.com/shop/account/get_brand_list?access_token={access_token}";
		$post=array('f_cat_id'=>0);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('获取品牌失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['brands'];
	}
	//查询运费模版
	public function get_freight_template() {
		$url = "https://api.weixin.qq.com/shop/delivery/get_freight_template?access_token={access_token}";
		$res=$this->request($url,array('from'=>'jy'));
		if($res['errcode']){
			exi('获取查询运费模版失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['template_list'];
	}
	//添加商品
	public function add_goods($goods,$product_id=0){
		if(!$product_id){
			$url = "https://gw-api.pinduoduo.com/api/router/ec/product/add?access_token={access_token}";
		}else{
			$url = "https://gw-api.pinduoduo.com/api/router/ec/product/update?access_token={access_token}";
		}
		$cats=array();
		$cats[]=array(
			'cat_id'=>$goods['api_data']['cate_id'],
			'level'=>1,
		);
		$goods['images']=json_decode($goods['images'],true);
		foreach($goods['images'] as &$mv){
			$mv=$mv?$this->upload_media($mv):'';
		}
		$goods['image']=$goods['image']?$this->upload_media($goods['image']):'';
		$goods['images'][]=$goods['image'];		
		$attrs=array();
		$attrs[]=array(
			'attr_key'=>'商品毛重',
			'attr_value'=>$goods['weight']?$goods['weight'].'g':'100g'
		);
		$skus=array();
		$skus[]=array(
			'out_product_id'=>$goods['id'],
			'out_sku_id'=>0,
			'thumb_img'=>$goods['image'],
			'sale_price'=>$goods['price']*100,
			'market_price'=>$goods['marketprice']*100,
			'stock_num'=>$goods['total'],
		);
		$brand_info=pdo_getcolumn('jy_duodianb_cate',array('cat_id'=>$goods['api_data']['cat_id'],'f_cat_id'=>$goods['api_data']['cate_1'],'second_cat_id'=>$goods['api_data']['cate_2'],'uniacid'=>$_SESSION['uniacid']),'brand_info');
		$brand_info=json_decode($brand_info,true);
		$brand_id=$brand_info['brand_id']?:0;
		$extra_service=array(
			'seven_day_return'=>0,
			'pay_after_use'=>0,
			'freight_insurance'=>0,
			'fake_one_pay_three'=>0,
			'damage_guarantee'=>0,
		);
		$newgoods=array(
			'out_product_id'=>$goods['id'],
			'title'=>$goods['title'],
			'sub_title'=>$goods['subtitle']?:$goods['shorttitle'],
			'path'=>'/jy_weishop/goods/detail/index?id='.$goods['id'],
			'head_imgs'=>$goods['images'],
			'deliver_method'=>$goods['type']==1?0:1,
			'head_imgs'=>$goods['images'],
			'desc_info'=>array('desc'=>$goods['content'],'imgs'=>$goods['images']),
			'brand_id'=>$brand_id?:2100000000,
			'cats'=>ijson_encode($cats),
			//'attrs'=>ijson_encode($attrs),
			//'express_info'=>ijson_encode(array('template_id'=>$goods['api_data']['template_id'])),
			'spu_code'=>$goods['goodssn'],
			'extra_service'=>ijson_encode($extra_service),
			'skus'=>$skus,
		);
		if($product_id){
			$newgoods['product_id']=$product_id;
		}
		$newgoods=ijson_encode($newgoods);
		//print_r($newgoods);exit;
		$res=$this->request($url,$newgoods,'','','',true);
		if($res['errcode']){
			exi('添加商品失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['data'];
	}
	public function del_goods($goodsid=0) {
		$url = "https://api.weixin.qq.com/shop/spu/del?access_token={access_token}";
		$post=array(
			'out_product_id'=>$goodsid,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('删除商品失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//获取所有商品
	public function get_allgoods($page=1) {
		$url = "https://api.weixin.qq.com/shop/spu/get_list?access_token={access_token}";
		$post=array(
			'page'=>$page,
			'page_size'=>99,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('获取商品失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['spus'];
	}
	//上架
	public function status1_goods($goodsid=0) {
		$url = "https://gw-api.pinduoduo.com/api/router/ec/product/listing?access_token={access_token}";
		$post=array(
			'out_product_id'=>$goodsid,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('上架商品失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//下架
	public function status0_goods($goodsid=0){
		$url = "https://gw-api.pinduoduo.com/api/router/ec/product/delisting?access_token={access_token}";
		$post=array(
			'out_product_id'=>$goodsid,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('下架商品失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//类目审
	public function audit_category($category_info) {
		$url = "https://api.weixin.qq.com/shop/audit/audit_category?access_token={access_token}";
		foreach($category_info['license'] as &$lv){
			$lv=$this->upload_media($lv);
		}
		foreach($category_info['certificate'] as &$cv){
			$cv=$this->upload_media($cv);
		}
		$post=array(
			'audit_req'=>array(
				'license'=>$category_info['license'],
				'category_info'=>array(
					'level1'=>$category_info['level1'],
					'level2'=>$category_info['level2'],
					'level3'=>$category_info['level3'],
					'certificate'=>$category_info['certificate'],
				),
			)
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('申请类目审核失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['audit_id'];
	}
	public function lookaudit($audit_id) {
		$url = "https://api.weixin.qq.com/shop/audit/result?access_token={access_token}";
		$post=array(
			'audit_id'=>$audit_id,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('查看类目审核失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['data'];
	}
	//上传图片
	public function upload_media($source){
		$source=tomedia($source);
		$post=array('img_url'=>$source);
		$img_info=@getimagesize($source);
		$width=$img_info[0];
		$height=$img_info[1];
		$url = "https://api.weixin.qq.com/shop/img/upload?access_token={access_token}&upload_type=1&resp_type=1&width=".$width."&height=".$height;
		$content = $this->request($url,$post);
		if($content['errcode']){
			exi('上传图片失败,错误详情：'.$content['errmsg'],'error');
		}
		return $content['img_info']['temp_img_url'];
	}
	//生成订单
	public function order_add($orderid,$tid=''){
		$url = "https://api.weixin.qq.com/shop/order/add?access_token={access_token}";
		$order_info=model('jy_weishop/order')->getorder_infodate($orderid,$tid);
		$order_info['scene']=1175;
		$order_info=ijson_encode($order_info);
		$res=$this->request($url,$order_info);
		if($res['errcode']){
			exi('生成订单失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['data']['order_id'];
	}
	//获取支付参数
	public function getpaymentparams($orderid,$apiorder_id,$tid=''){
		$url = "https://api.weixin.qq.com/shop/order/getpaymentparams?access_token={access_token}";
		$post=array(
			'order_id'=>$apiorder_id,
			'out_order_id'=>$tid,
			'openid'=>$_SESSION['openid'],
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('获取支付参数失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['payment_params'];
	}
	//支付同步
	public function order_pay($order){
		exi('新接口不需要，同步支付了！','error');
		$url = "https://api.weixin.qq.com/shop/order/pay?access_token={access_token}";
		if(!$_SESSION['openid']){
			$fans=cfc('fans')->get_fansinfo($order['mid'],1);
			$openid=$fans['openid'];
		}
		$tid=pdo_getcolumn('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
		$post=array(
			'out_order_id'=>$tid?:$order['id'],
			'openid'=>$openid?:$_SESSION['openid'],
			'action_type'=>1,
			'transaction_id'=>$order['transid'],
			'pay_time'=>date('Y-m-d H:i:s'),
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post,true,'',true);
		if($res['errcode']){
			if($res['errcode']==1000000){
				exi('订单支付状态已同步，请勿重复同步！','error');
			}
			exi('支付同步失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//获取订单详情
	public function get_orderitem($order){
		$url = "https://api.weixin.qq.com/shop/order/get?access_token={access_token}";
		$fans=cfc('fans')->get_fansinfo($order['mid'],1);
		$tid=pdo_getcolumn('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
		$post=array(
			'out_order_id'=>$tid?:$order['id'],
			'openid'=>$fans['openid'],
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post,true,false,false,true);
		if($res['errcode']){
			exi('获取订单失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['order'];
	}
	//获取订单
	public function get_orders($params){
		$url = "https://api.weixin.qq.com/shop/order/get_list?access_token={access_token}";
		$post=array(
			'start_create_time'=>$params['start_create_time']?:'2022-01-01 01:00:00',
			'end_create_time'=>$params['end_create_time']?:date('Y-m-d H:i:s',time()),
			'status'=>$params['status']?:20,
			'page'=>$params['page']?:1,
			'page_size'=>$params['page_size']?:50,
			'source'=>$params['source']?:1,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post,true,false,false,true);
		//print_r($res);exit;
		if($res['errcode']){
			exi('获取订单失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['orders'];
	}
	//创建售后：refundtype:'0'=>'退货退款','1'=>'换货','2'=>'退货不退款','3'=>'退款不退货','4'=>'退运费';
	public function add_aftersale($refund,$orderstatus=1){
		$url = "https://api.weixin.qq.com/shop/ecaftersale/add?access_token={access_token}";
		$order=pdo_get('jy_weishop_order',array('id'=>$refund['orderid'],'uniacid'=>$_SESSION['uniacid']),array('oldstatus','status','pay_id'));
		$order['status']=$order['oldstatus']?:$order['status'];
		if($order['status']==1){
			$refund_reason_type=3;
			$type=1;
		}else{
			$refund_reason_type=8;
			$type=2;
		}
		if($refund['status']==0){
			$status=2;
		}elseif($refund['status']==1){
			$status=11;
		}elseif($refund['status']==2){
			$status=8;
		}elseif($refund['status']==3){
			$status=15;
		}elseif($refund['status']==4){
			$status=13;
		}elseif($refund['status']==5){
			$status=4;
		}
		$goods=array();
		$goodsall=pdo_getall('jy_weishop_order_goods',array('orderid'=>$refund['orderid']));
		foreach($goodsall as $item){
			$goods[]=array(
				'out_product_id'=>$item['goodsid'],
				'out_sku_id'=>$item['optionid'],
				'product_cnt'=>intval($item['total']),
			);
		}
		if(!$_SESSION['openid']){
			$fans=cfc('fans')->get_fansinfo($refund['mid'],1);
			$openid=$fans['openid'];
		}
		$tid=pdo_getcolumn('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
		$post=array(
			'out_order_id'=>$tid?:$refund['orderid'],
			'out_aftersale_id'=>$refund['id'],
			'path'=>'/jy_weishop/order/detail/index?id='.$refund['orderid'],
			'refund'=>$refund['price']*100,
			'openid'=>$openid?:$_SESSION['openid'],
			'type'=>$type,
			'create_time'=>date('Y-m-d H:i:s',$refund['createtime']),
			'status'=>$status,
			'finish_all_aftersale'=>$status==4?1:0,
			'refund_reason'=>$refund['content']?:'无',
			'refund_reason_type'=>$refund_reason_type,
			'orderamt'=>$refund['price']*100,
			'refund_address'=>'',
			'product_info'=>$goods[0],
		);
		//print_r($post);exit;
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('更新售后失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//更新售后
	public function up_aftersale($refund){
		$url = "https://api.weixin.qq.com/shop/ecaftersale/update?access_token={access_token}";
		if($refund['status']==0){
			$status=2;
		}elseif($refund['status']==1){
			$status=11;
		}elseif($refund['status']==2){
			$status=8;
		}elseif($refund['status']==3){
			$status=15;
		}elseif($refund['status']==4){
			$status=13;
		}elseif($refund['status']==5){
			$status=4;
		}
		if(!$_SESSION['openid']){
			$fans=cfc('fans')->get_fansinfo($refund['mid'],1);
			$openid=$fans['openid'];
		}
		$pay_id=pdo_getcolumn('jy_weishop_order',array('id'=>$refund['orderid'],'uniacid'=>$_SESSION['uniacid']),'pay_id');
		$tid=pdo_getcolumn('core_paylog',array('id'=>$pay_id,'uniacid'=>$_SESSION['uniacid']),'tid');
		$post=array(
			'out_order_id'=>$tid?:$refund['orderid'],
			'out_aftersale_id'=>$refund['id'],
			'openid'=>$openid?:$_SESSION['openid'],
			'status'=>$status,
			'finish_all_aftersale'=>$status==4?1:0,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('更新售后失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//同意退款
	public function acceptrefund($refund){
		$url = "https://api.weixin.qq.com/shop/ecaftersale/acceptrefund?access_token={access_token}";
		$post=array(
			'out_aftersale_id'=>$refund['id'],
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('同意退款失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//发货
	public function add_delivery($orderid,$data=array()){
		$url = "https://api.weixin.qq.com/shop/delivery/send?access_token={access_token}";
		$order=pdo_get('jy_weishop_order',array('id'=>$orderid));
		if(!$_SESSION['openid']){
			$fans=cfc('fans')->get_fansinfo($order['mid'],1);
			$openid=$fans['openid'];
		}
		$goodsall=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		$goods=array();
		foreach($goodsall as $item){
			$goods[]=array(
				'out_product_id'=>$item['goodsid'],
				'out_sku_id'=>$item['optionid'],
				'product_cnt'=>intval($item['total']),
			);
		}
		$tid=pdo_getcolumn('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
		$delivery_id=pdo_getcolumn('jy_duodian_express',array('id'=>$data['express']),'name');
		$post=array(
			'out_order_id'=>$tid?:$orderid,
			'openid'=>$openid?:$_SESSION['openid'],
			'finish_all_delivery'=>1,
			'delivery_list'=>array(
				array(
					'delivery_id'=>$delivery_id,
					'waybill_id'=>$data['expresssn'],
					'product_info_list'=>$goods,
				)
			),
			'ship_done_time'=>date('Y-m-d H:i:s'),
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('发货失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//确认收货
	public function recieve_delivery($orderid,$isnoexi=false){
		$url = "https://api.weixin.qq.com/shop/delivery/recieve?access_token={access_token}";
		$order=pdo_get('jy_weishop_order',array('id'=>$orderid));
		if(!$_SESSION['openid']){
			$fans=cfc('fans')->get_fansinfo($order['mid'],1);
			$openid=$fans['openid'];
		}
		$tid=pdo_getcolumn('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
		$post=array(
			'out_order_id'=>$tid?:$orderid,
			'openid'=>$openid?:$_SESSION['openid'],
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode'] && !$isnoexi){
			exi('收货失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	public function get_company_list() {
		$url = "https://api.weixin.qq.com/shop/delivery/get_company_list?access_token={access_token}";
		$post=array(
			'is_jy'=>1,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('获取快递公司失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res['company_list'];
	}
	//更新商家信息
	public function update_info($postdata) {
		$url = "https://api.weixin.qq.com/shop/account/update_info?access_token={access_token}";
		$default_receiving_address=array(
			'receiver_name'=>$postdata['receiver_name'],
			'detailed_address'=>$postdata['detailed_address'],
			'tel_number'=>$postdata['tel_number'],
			'country'=>$postdata['country'],
			'province'=>$postdata['pcd']['province'],
			'city'=>$postdata['pcd']['city'],
			'town'=>$postdata['pcd']['district'],
		);
		$post=array(
			'service_agent_path'=>$postdata['service_agent_path'],
			'service_agent_phone'=>$postdata['service_agent_phone'],
			'service_agent_type'=>str_array($postdata['service_agent_type']),
			'default_receiving_address'=>$default_receiving_address,
		);
		$post=ijson_encode($post);
		$res=$this->request($url,$post);
		if($res['errcode']){
			exi('更新商家信息失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return true;
	}
	//错误码
	public function error_code($code, $errmsg = '未知错误') {//获取错误详情
		global $_AR;
		$errors = array(
			'-1' => '系统繁忙',
			'0' => '请求成功',
			'100010'=>'类目和品牌不匹配（目前默认是无品牌）',
			'300002' => '商品标题太长了！最长14个汉字',
			'300003' => '价格输入不合规（如：现价比原价大、传入价格非数字等）',
			'300018' => '图片尺寸超过300px*300px',
			'300006' => '图片上传失败（如：mediaID过期）',
			'300007' => '线上小程序版本不存在该链接,请重新上传小程序代码！',
			'300022'=>'此房间号不存在',
			'300023'=>'房间状态 拦截（当前房间状态不允许此操作）',
			'300024'=>'商品不存在',
			'300025'=>'商品审核未通过',
			'300026'=>'房间商品数量已经满额',
			'300027'=>'导入商品失败',
			'300028'=>'房间名称违规',
			'300029'=>'主播昵称违规',
			'300030'=>'主播微信号不合法',
			'300031'=>'直播间封面图不合规',
			'300032'=>'直播间分享图违规',
			'300033'=>'添加商品超过直播间上限',
			'300034'=>'主播微信昵称长度不符合要求',
			'300035'=>'主播微信号不存在',
			'300036'=>'主播微信号未实名认证,登录微信搜索--《直播小程序》--实名认证一下即可！',
			'40001' => '获取access_token时AppSecret错误，或者access_token无效',
			'40002' => '不合法的凭证类型',
			'40003' => '不合法的OpenID',
			'40004' => '不合法的媒体文件类型',
			'40005' => '不合法的文件类型',
			'40006' => '不合法的文件大小',
			'40007' => '不合法的媒体文件id',
			'40008' => '不合法的消息类型',
			'40009' => '不合法的图片文件大小',
			'40010' => '不合法的语音文件大小',
			'40011' => '不合法的视频文件大小',
			'40012' => '不合法的缩略图文件大小',
			'40013' => '不合法的APPID',
			'40014' => '不合法的access_token',
			'40015' => '不合法的菜单类型',
			'40016' => '不合法的按钮个数',
			'40017' => '不合法的按钮个数',
			'40018' => '不合法的按钮名字长度',
			'40019' => '不合法的按钮KEY长度',
			'40020' => '不合法的按钮URL长度',
			'40021' => '不合法的菜单版本号',
			'40022' => '不合法的子菜单级数',
			'40023' => '不合法的子菜单按钮个数',
			'40024' => '不合法的子菜单按钮类型',
			'40025' => '不合法的子菜单按钮名字长度',
			'40026' => '不合法的子菜单按钮KEY长度',
			'40027' => '不合法的子菜单按钮URL长度',
			'40028' => '不合法的自定义菜单使用用户',
			'40029' => '不合法的oauth_code',
			'40030' => '不合法的refresh_token',
			'40031' => '不合法的openid列表',
			'40032' => '不合法的openid列表长度',
			'40033' => '不合法的请求字符，不能包含\uxxxx格式的字符',
			'40035' => '不合法的参数',
			'40038' => '不合法的请求格式',
			'40039' => '不合法的URL长度',
			'40050' => '不合法的分组id',
			'40051' => '分组名字不合法',
			'41001' => '缺少access_token参数',
			'41002' => '缺少appid参数',
			'41003' => '缺少refresh_token参数',
			'41004' => '缺少secret参数',
			'41005' => '缺少多媒体文件数据',
			'41006' => '缺少media_id参数',
			'41007' => '缺少子菜单数据',
			'41008' => '缺少oauth code',
			'41009' => '缺少openid',
			'42001' => 'access_token超时',
			'42002' => 'refresh_token超时',
			'42003' => 'oauth_code超时',
			'43001' => '需要GET请求',
			'43002' => '需要POST请求',
			'43003' => '需要HTTPS请求',
			'43004' => '需要接收者关注',
			'43005' => '需要好友关系',
			'44001' => '多媒体文件为空',
			'44002' => 'POST的数据包为空',
			'44003' => '图文消息内容为空',
			'44004' => '文本消息内容为空',
			'45001' => '多媒体文件大小超过限制',
			'45002' => '消息内容超过限制',
			'45003' => '标题字段超过限制',
			'45004' => '描述字段超过限制',
			'45005' => '链接字段超过限制',
			'45006' => '图片链接字段超过限制',
			'45007' => '语音播放时间超过限制',
			'45008' => '图文消息超过限制',
			'45009' => '接口调用超过限制',
			'45010' => '创建菜单个数超过限制',
			'45015' => '回复时间超过限制',
			'45016' => '系统分组，不允许修改',
			'45017' => '分组名字过长',
			'45018' => '分组数量超过上限',
			'45056' => '创建的标签数过多，请注意不能超过100个',
			'45057' => '该标签下粉丝数超过10w，不允许直接删除',
			'45058' => '不能修改0/1/2这三个系统默认保留的标签',
			'45059' => '有粉丝身上的标签数已经超过限制',
			'45157' => '标签名非法，请注意不能和其他标签重名',
			'45158' => '标签名长度超过30个字节',
			'45159' => '非法的标签',
			'46001' => '不存在媒体数据',
			'46002' => '不存在的菜单版本',
			'46003' => '不存在的菜单数据',
			'46004' => '不存在的用户',
			'47001' => '解析JSON/XML内容错误',
			'48001' => 'api功能未授权',
			'50001' => '用户未授权该api',
			'40070' => '基本信息baseinfo中填写的库存信息SKU不合法。',
			'41011' => '必填字段不完整或不合法，参考相应接口。',
			'40056' => '无效code，请确认code长度在20个字符以内，且处于非异常状态（转赠、删除）。',
			'43009' => '无自定义SN权限，请参考开发者必读中的流程开通权限。',
			'43010' => '无储值权限,请参考开发者必读中的流程开通权限。',
			'43011' => '无积分权限,请参考开发者必读中的流程开通权限。',
			'40078' => '无效卡券，未通过审核，已被置为失效。',
			'40079' => '基本信息base_info中填写的date_info不合法或核销卡券未到生效时间。',
			'45021' => '文本字段超过长度限制，请参考相应字段说明。',
			'40080' => '卡券扩展信息cardext不合法。',
			'40097' => '基本信息base_info中填写的参数不合法。',
			'49004' => '签名错误。请联系管理员设置好参数！',
			'43012' => '无自定义cell跳转外链权限，请参考开发者必读中的申请流程开通权限。',
			'40099' => '该code已被核销。',
			'61005' => '缺少接入平台关键数据，等待微信开放平台推送数据，请十分钟后再试或是检查“授权事件接收URL”是否写错（index.php?c=account&amp;a=auth&amp;do=ticket地址中的&amp;符号容易被替换成&amp;amp;）',
			'61023' => '请重新授权接入该小程序',
			'85005'=>'小程序未关联小程序，无法通过自定义菜单跳转！',
			'40164'=>'服务器IP不在白名单导致接口请求失败！请前往公众平台——开发——基本配置——小程序开发信息——IP白名单处设置',
			'9401001' => 'SPU已经存在',
			'9401002' => 'SPU不存在',
			'9401020' => '参数有误',
			'9401021' => '无权限调用该api',
			'9401023' => 'SPU不允许编辑',
			'9401026' => '商品的类目长度不对（预期是有三级类目）',
			'9401032' => '类目非法',
			'9401033' => '商家不具备当前类目资质',
			'9401034' => '运费模版idexpress_info.template_id非法',
			'9401035' => '商品标题title为空',
			'9401036' => '商品标题title字符过长',
			'9401037' => '商品主图head_img为空',
			'9401038' => '商品主图head_img过多',
			'9401039' => '副标题sub_title字符过长',
			'9401040' => '商品详情图片desc_info.imgs过多',
			'9401041' => '商品详情描述字符过长',
			'9401042' => '资质图片过多',
			'9401043' => 'SKU价格过高',
			'9401044' => 'SKU商品编码sku_code过长',
			'9401045' => '未知错误',
			'9401046' => '	SKU同key属性个数过多',
			'9401047' => 'sku属性键keyskus[].sku_attrs[].attr_key字符过长',
			'9401048' => 'sku属性值skus[].sku_attrs[].attr_value字符过长',
			'9401049' => '售后说明字符过长',
			'9401050' => '图片url非法（请参考文档的【注意】）',
			'9401051' => 'out_product_id过长',
			'9401052' => 'out_sku_id过长',
		);
		$code = strval($code);
		if($errors[$code]) {
			return $errors[$code];
		} else {
			return $errmsg;
		}
	}
}